import {emitType} from "./GameData_red_blue_man";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Camera_control extends cc.Component {

    camera = null;

    _cameraMaxX = 0;
    _cameraMaxY = 0;

    mapSize = null;

    //主角一
    hero1 = null;
    //主角二
    hero2 = null;

    //正在使用的角色
    playingMan = null;
    //是否在使用相机的缩放看大地图
    isScaleMap = false;
    //没帧变化的值
    scaleNum = 0;

    onLoad () {
        cc.director.getPhysicsManager().enabled = true;
        // cc.director.getPhysicsManager().debugDrawFlags = 1;

        this.camera = this.node.getComponent(cc.Camera);

        cc.game.on(emitType.cutMan, this.setNode, this);
    }

    start () {
        this.schedule(this.cameraControl, 0.01);
    }

    //宽缩放
    widthScale = 0;
    //高缩放
    heightScale = 0;
    //最终缩放值
    scaleValue = 0;
    getMapSize(data){
        this.mapSize = data.mapSize;
        this.hero1 = data.hero1;
        this.hero2 = data.hero2;
        this.playingMan = this.hero1;

        this.widthScale = cc.winSize.width/this.mapSize.width;
        this.heightScale = cc.winSize.width/this.mapSize.width;
        this.scaleValue = this.widthScale > this.heightScale ? this.heightScale : this.widthScale;
        this.scaleNum = (1-this.scaleValue)/20;
        this.setCameraScope();
    }

    //做相机的缩放
    scaleCamera(state){
        this.isScaleMap = state;
        if(state){
            this.unschedule(this.cameraAnimSmall);
            this.node.runAction(
                cc.moveTo(0.3, cc.v2(0,0)),
            );
            this.cameraAnimBig();
            this.schedule(this.cameraAnimBig,0.01)
        }else{
            this.cameraAnimSmall();
            this.unschedule(this.cameraAnimBig);
            this.schedule(this.cameraAnimSmall, 0.01);
        }
    }

    cameraAnimBig(){
        this.camera.zoomRatio -= this.scaleNum;
        if(this.camera.zoomRatio <= this.scaleValue){
            this.camera.zoomRatio = this.scaleValue;
            this.unschedule(this.cameraAnimBig);
        }
    }
    cameraAnimSmall(){
        this.camera.zoomRatio += this.scaleNum;
        if(this.camera.zoomRatio >= 1){
            this.camera.zoomRatio = 1;
            this.unschedule(this.cameraAnimSmall);
        }
    }

    setCameraScope() {
        this._cameraMaxX = (this.mapSize.width - cc.winSize.width)/2;
        this._cameraMaxY = (this.mapSize.height - cc.winSize.height)/2;
    }

    setNode(node){
        this.playingMan = node;
        this.setCameraScope();
    }

    cameraControl(){
        if(this.playingMan == null || this.isScaleMap){
            return;
        }
        let target = this.playingMan.position;
        if (target.x > this._cameraMaxX) {
            target.x = this._cameraMaxX;
        }
        if (target.x < -this._cameraMaxX) {
            target.x = -this._cameraMaxX;
        }
        if (target.y > this._cameraMaxY) {
            target.y = this._cameraMaxY;
        }
        if (target.y < -this._cameraMaxY) {
            target.y = -this._cameraMaxY;
        }

        let disX = target.x - this.node.x;
        let disY = target.y - this.node.y;
        if(Math.abs(disX) > 20){
            this.node.x += disX/20;
        }
        if(Math.abs(disY) > 20){
            this.node.y += disY/20;
        }
    }

    update (dt) {

    }
}
